home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / class / findfile.d < prev    next >
Text File  |  1997-04-15  |  4KB  |  224 lines

  1.  
  2. /*
  3.  *
  4.  *    Copyright (c) 1993-1996 Algorithms Corporation
  5.  *    3020 Liberty Hills Drive
  6.  *    Franklin, TN  37067
  7.  *
  8.  *    ALL RIGHTS RESERVED.
  9.  *
  10.  *
  11.  *
  12.  */
  13.  
  14.  
  15. #if  defined(_WIN32)  ||  defined(WIN32)
  16. #include <io.h>
  17. #define    FINDSTRUCT    _finddata_t
  18. #ifndef    _WIN32
  19. #define    _WIN32
  20. #endif
  21. #else
  22. #include <dos.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #define    FINDSTRUCT    find_t
  26. #endif
  27.  
  28.  
  29. #include "findfile.h"
  30.  
  31.  
  32.  
  33. defclass  FindFile  {
  34.     iFileName;
  35.     unsigned    iAttributes;    //  those defined by Dynace
  36.     struct    FINDSTRUCT    iData;
  37.     long    iHandle;    //  only used by WIN32
  38.     int    iFirst;        //  1=findfirst done already
  39.     int    iEnd;        //  1=seen them all
  40. };
  41.  
  42. cmeth    gNewFindFile, <vNew> (char *name, int attr)
  43. {
  44.     object    obj = gNew(super);
  45.     ivType    *iv = ivPtr(obj);
  46.     iFileName = gNewWithStr(String, name);
  47.     iAttributes = attr;
  48.     iHandle = -1L;
  49.     return obj;
  50. }
  51.  
  52. imeth    gDispose, gDeepDispose ()
  53. {
  54. #ifdef    _WIN32
  55.     if (iHandle != -1L)
  56.         _findclose(iHandle);
  57. #endif
  58.     gDispose(iFileName);
  59.     return gDispose(super);
  60. }
  61.  
  62. imeth    gGCDispose()
  63. {
  64. #ifdef    _WIN32
  65.     if (iHandle != -1L)
  66.         _findclose(iHandle);
  67. #else
  68.     USE(iHandle);
  69. #endif
  70.     return gDispose(super);
  71. }
  72.  
  73. #define    SET(a, b)    ((a) & (b))
  74.  
  75. static    int    valid(ivType *iv)
  76. {
  77.     if (SET(iData.attrib, _A_SUBDIR)) {
  78.         if (!SET(iAttributes, FF_DIRECTORY))
  79.             return 0;
  80.     } else
  81.         if (!SET(iAttributes, FF_FILE))
  82.             return 0;
  83.     if (SET(iData.attrib, _A_RDONLY)) {
  84.         if (!SET(iAttributes, FF_READONLY))
  85.             return 0;
  86.     } else
  87.         if (!SET(iAttributes, FF_READWRITE))
  88.             return 0;
  89.     if (SET(iData.attrib, _A_HIDDEN)  &&  !SET(iAttributes, FF_HIDDEN))
  90.         return 0;
  91.     if (SET(iData.attrib, _A_SYSTEM)  &&  !SET(iAttributes, FF_SYSTEM))
  92.         return 0;
  93.     if (SET(iAttributes, FF_ARCHIVE_ONLY)  &&  !SET(iData.attrib, _A_ARCH))
  94.         return 0;
  95.     return 1;
  96. }
  97.  
  98. imeth    char    *gNextFile()
  99. {
  100. #ifdef    _WIN32
  101.     int    r;
  102.     
  103.     if (iEnd)
  104.         return NULL;
  105.     if (!iFirst) {
  106.         iHandle = _findfirst(gStringValue(iFileName), &iData);
  107.         iFirst = 1;
  108.         r = iHandle < 0L ? 1 : 0;
  109.     } else
  110.         r = _findnext(iHandle, &iData);
  111.     while (!r  &&  !valid(iv))
  112.         r = _findnext(iHandle, &iData);
  113.     if (r) {
  114.         iEnd = 1;
  115.         if (iHandle != -1L) {
  116.             _findclose(iHandle);
  117.             iHandle = -1;
  118.         }
  119.         return NULL;
  120.     } else
  121.         return iData.name;
  122. #else    
  123.     unsigned   r;
  124.     
  125.     if (iEnd)
  126.         return NULL;
  127.     if (!iFirst) {
  128.         r = _dos_findfirst(gStringValue(iFileName),
  129.                    _A_ARCH | _A_HIDDEN | _A_NORMAL | _A_RDONLY | _A_SUBDIR | _A_SYSTEM,
  130.                    &iData);
  131.         iFirst = 1;
  132.     } else
  133.         r = _dos_findnext(&iData);
  134.     while (!r  &&  !valid(iv))
  135.         r = _dos_findnext(&iData);
  136.     if (r) {
  137.         iEnd = 1;
  138.         return NULL;
  139.     } else
  140.         return iData.name;
  141. #endif
  142. }
  143.  
  144. imeth    long    gLength()
  145. {
  146.     return iFirst && !iEnd ? iData.size : -1L;
  147. }
  148.  
  149. imeth    char    *gName()
  150. {
  151.     return  iFirst && !iEnd ? iData.name : NULL;
  152. }
  153.  
  154. #ifndef    _WIN32
  155. static    void    getpath(char *path, char *search, char *name)
  156. {
  157.     char    *p, *last;
  158.  
  159.     strcpy(path, search);
  160.     for (last=path-1, p=path ;  *p  ;  ++p)
  161.         if (*p == ':'  ||  *p == '/'  ||  *p == '\\')
  162.             last = p;
  163.     strcpy(last+1, name);
  164. }
  165. #endif
  166.  
  167. imeth    long    gWriteTime()
  168. {
  169. #ifdef    _WIN32
  170.     if (!iFirst  ||  iEnd)
  171.         return -1L;
  172.     return iData.time_write;
  173. #else
  174.     char    path[256];
  175. #ifdef    MSC16
  176.     struct    _stat    sb;
  177. #else
  178.     struct    stat    sb;
  179. #endif
  180.     
  181.     if (!iFirst  ||  iEnd)
  182.         return -1L;
  183.     getpath(path, gStringValue(iFileName), iData.name);
  184.     return _stat(path, &sb) ? -1L : sb.st_mtime;
  185. #endif
  186. }
  187.  
  188. imeth    unsigned    gAttributes()
  189. {
  190.     unsigned    at = 0;
  191.     
  192.     if (!iFirst  ||  iEnd)
  193.         return at;
  194.     if (SET(iData.attrib, _A_SUBDIR))
  195.         at |= FF_DIRECTORY;
  196.     else
  197.         at |= FF_FILE;
  198.     if (SET(iData.attrib, _A_RDONLY))
  199.         at |= FF_READONLY;
  200.     else
  201.         at |= FF_READWRITE;
  202.     if (SET(iData.attrib, _A_HIDDEN))
  203.         at |= FF_HIDDEN;
  204.     if (SET(iData.attrib, _A_SYSTEM))
  205.         at |= FF_SYSTEM;
  206.     if (SET(iData.attrib, _A_ARCH))
  207.         at |= FF_ARCHIVE_ONLY;
  208.     return at;
  209. }
  210.  
  211.  
  212.  
  213. /*
  214.  *
  215.  *    Copyright (c) 1993-1996 Algorithms Corporation
  216.  *    3020 Liberty Hills Drive
  217.  *    Franklin, TN  37067
  218.  *
  219.  *    ALL RIGHTS RESERVED.
  220.  *
  221.  *
  222.  *
  223.  */
  224.